package org.go.workstore;

import java.util.List;

import org.go.domain.CompletedExecutionInstruction;
import org.go.domain.TriggerType;
import org.go.expcetion.GoException;
import org.go.expcetion.JobPersistenceException;
import org.go.expcetion.ObjectAlreadyExistsException;
import org.go.spi.TriggerFiredResult;
import org.go.trigger.Calendar;
import org.go.trigger.Trigger;
import org.go.work.Work;
import org.go.work.WorkKey;

/**
 * 
 * @author hejie
 *
 */
public interface WorkStore {
	/**
	 * 
	 * @param noLaterThan
	 * @param maxCount
	 * @param timeWindow
	 * @return
	 * @throws GoException
	 * @throws JobPersistenceException 
	 */
	List<Trigger> acquireNextTriggers(long noLaterThan, int maxCount, long timeWindow) throws GoException, JobPersistenceException;

	/**
	 * 
	 * @param workerDesc
	 */
	void addWorkDesc(Work workerDesc);

	Work queryWorkDesc(String id);

	/**
	 * 
	 * @param type
	 * @return
	 */
	Work[] queryWorkDesc(TriggerType type);

	/**
	 * 
	 * @param trigger
	 * @throws GoException
	 * @throws JobPersistenceException
	 */
	void releaseAcquiredTrigger(Trigger trigger) throws GoException, JobPersistenceException;

	Calendar retrieveCalendar(String calName) throws JobPersistenceException;

	Work retrieveJob(WorkKey jobKey) throws JobPersistenceException;

	/**
	 * Store the given <code>{@link org.go.Trigger}</code>.
	 *
	 * @param newTrigger
	 *          The <code>Trigger</code> to be stored.
	 * @param replaceExisting
	 *          If <code>true</code>, any <code>Trigger</code> existing in
	 *          the <code>JobStore</code> with the same name & group should
	 *          be over-written.
	 * @throws JobPersistenceException
	 *           if a <code>Trigger</code> with the same name/group already
	 *           exists, and replaceExisting is set to false.
	 * @throws ObjectAlreadyExistsException 
	 *
	 * @see #pauseTriggers(org.go.impl.matchers.GroupMatcher)
	 */
	void storeTrigger(Trigger newTrigger, boolean replaceExisting) throws JobPersistenceException, ObjectAlreadyExistsException;

	boolean supportsPersistence();

	void triggeredJobComplete(Trigger trigger, Object jobDetail, CompletedExecutionInstruction setAllJobTriggersError) throws JobPersistenceException;

	List<TriggerFiredResult> triggersFired(List<Trigger> triggers) throws GoException, JobPersistenceException;
}
